package cn.date;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public final class DateHelper {
	private DateHelper() {
		super();
	}
	public static DateHelper getInstance() {
		DateHelper DH=new DateHelper();
		return DH;
	}
	public Date toDate( int year , int month , int date , int hourOfDay , int minute ) {
		Calendar c1=new GregorianCalendar();
		c1.set(year, month, date, hourOfDay, minute,0);
		c1.set(Calendar.MILLISECOND,0);
		Date d1=c1.getTime();
		return d1;
		// 可以将 秒 和 毫秒 部分直接设置为 零
	}

	public Date toDate( int year , int month , int date , int hourOfDay , int minute , int second ) {
		Calendar c2=new GregorianCalendar();
		c2.set(year, month, date, hourOfDay, minute,second);
		c2.set(Calendar.MILLISECOND,0);
		Date d2=c2.getTime();
		return d2;
		// 可以将 毫秒 部分直接设置为 零
	}

	public Date toDate( int year , int month , int date ,int hourOfDay , int minute , int second , int millis ) {
		// 自行设计实现过程
		Calendar c3=new GregorianCalendar();
		c3.set(year, month, date, hourOfDay, minute,second);
		long millisNow=c3.getTimeInMillis()+millis;
		Date d3=new Date(millisNow);
		return d3;
	}
	public String toString( final Date date ) {
		final String pattern="Gyyyy年MM月dd日 EE HH:mm:ss.SSS" ;
		DateFormat sd=new SimpleDateFormat(pattern);
		String str=sd.format(date);
		 /* Calendar calendar=new GregorianCalendar();
		  calendar.setTime(date);
		  int year=calendar.get(Calendar.YEAR);
		  int month=calendar.get(Calendar.MONTH);
		  int dateNow=calendar.get(Calendar.DATE);
		  int hourOfDay=calendar.get(Calendar.HOUR_OF_DAY);
		  int minute=calendar.get(Calendar.MINUTE);
		  int second=calendar.get(Calendar.SECOND);
		  long millis=calendar.get(Calendar.MILLISECOND);
		// 从 Calendar实例 中 获取 各个日历字段 的值 并 连接到 字符序列 中
		  String str=year+"年"+month+"月"+dateNow+"日"+hourOfDay+":"+minute+":"+second+"."+millis;*/
		  return str;// 返回 Calendar实例 对应的字符序列
	}
	public int durationOfDays( Date first , Date second ) {
		int day;
		Calendar c1=new GregorianCalendar();
		Calendar c2=new GregorianCalendar();
		c1.setTime(first);
		c2.setTime(second);
		if(first.before(second)) {
			long l=first.getTime()-second.getTime();
			day=(int)(l/(1000*60*60*24));
			System.out.println("早了"+day+"天");
		}
		else if(first.equals(second)) {
			System.out.println("同一天");
			return 0;
		}
		else {
			long l=first.getTime()-second.getTime();
			day=(int)(l/(1000*60*60*24));
			System.out.println("晚了"+-day+"天");
		}
		return day;
		// 自行设计实现过程
	}

}
